/**
 * @file Body_Heath_Sensor.cpp
 * @brief human body health monitoring sensor
 * @author achdu0000
 * @email achdu0000@163.com
 * @version 1.0.0
 * @date 2024/07/01
 */

#include "BodyHealthSensor.h"

#define ONLY_FOR_HEART

bool isAvailable(BodyHeathSensor bhs)
{
    return (bhs.header == 0xFF && bhs.state == 0x01);
}

void myBodyHealthSensorSetup()
{
    _BODY_HEALTH_SERIAL.begin(38400);
    while (!_BODY_HEALTH_SERIAL)
        ;
}

void myBodyHeathSensorLoop()
{
    if (sendStartSeqFlag)
    {
        SEND_START_SEQ();
#ifndef ONLY_FOR_HEART
        Serial.println("_BODY_HEALTH_SERIAL.write(\"0x8A\")");
#endif
        sendStartSeqFlag = false;
    }

    if (_BODY_HEALTH_SERIAL.available())
    {
#ifndef ONLY_FOR_HEART
        Serial.println("_BODY_HEALTH_SERIAL.available()");
#endif
        BodyHeathSensor bhs;
        int data_len = _BODY_HEALTH_SERIAL.readBytes((uint8_t *)&bhs, sizeof(BodyHeathSensor));
#ifndef ONLY_FOR_HEART
        Serial.print("data_len: ");
        Serial.println(data_len);
#endif
        if (data_len != sizeof(BodyHeathSensor) || !isAvailable(bhs))
        {
#ifndef ONLY_FOR_HEART
            Serial.println("! data_len");
            return;
#else
            Serial.println(0);
            return;
#endif
        }

#ifndef ONLY_FOR_HEART
        Serial.print("数据头: ");
        Serial.println(bhs.header, HEX);
#endif
#ifndef ONLY_FOR_HEART

        Serial.print("心律波形数据: ");
#endif
        for (int i = 0; i < 64; i++)
        {
#ifndef ONLY_FOR_HEART
            Serial.print(bhs.acdata[i]);

            if (i < 63)
            {
                Serial.print(", ");
            }
#else
            Serial.println(bhs.acdata[i]);

#endif
        }

         
#ifndef ONLY_FOR_HEART
        Serial.println();

        Serial.print("心率: ");
        Serial.println(bhs.heartrate);

        Serial.print("血氧: ");
        Serial.println(bhs.spo2);

        Serial.print("微循环: ");
        Serial.println(bhs.bk);

        Serial.print("疲劳指数: ");
        Serial.println(bhs.fatig);

        Serial.print("收缩压: ");
        Serial.println(bhs.rsv3);

        Serial.print("舒张压: ");
        Serial.println(bhs.rsv4);

        Serial.print("心输出: ");
        Serial.println(bhs.rsv5);

        Serial.print("外周阻力: ");
        Serial.println(bhs.rsn6);

        Serial.print("RR间期1: ");
        Serial.println(bhs.rsv7);

        Serial.print("心率变异性: ");
        Serial.println(bhs.sdnn);

        Serial.print("RMSD: ");
        Serial.println(bhs.rmssd);

        Serial.print("NN50: ");
        Serial.println(bhs.nn50);

        Serial.print("PNN50: ");
        Serial.println(bhs.pnn50);

        Serial.print("RR间期数组: ");
        for (int i = 0; i < 6; i++)
        {
            Serial.print(bhs.rra[i]);
            if (i < 5)
            {
                Serial.print(", ");
            }
        }
        Serial.println();
#endif
        //    END();
        //    while(1);

        //    START();
    }
    else
    {
// #ifdef ONLY_FOR_HEART
//             Serial.println(0);
// #endif
    }
    delay(97);
}
